/*
                  KEFKA'S   REVENGE 
                  |=|  /=/    |=|  \=\
                  |=|/=/      |=|  /=/
                  |=|\=\      |=| \=\
                  |=|  \=\    |=|   \=\  .js 		         
                  ==============/
                      BY KEITH LINK   
              EMAIL: link1kr@cmich.edu
                 AIM: Thompson1189                                                                               
*/
GetSystemFont().drawText(15,10,"LOADING...");
FlipScreen();

RequireScript("clock.js");
RequireScript("properties.js");
RequireScript("scenes01_00_prechase.js");
RequireScript("scenes01_01_chasecourtjail.js");
RequireScript("scenes01_02_stuff_stfu.js");
RequireScript("scenes01_03_splitshit.js");
RequireScript("subevents.js");
RequireScript("menu.js");
RequireScript("bootmaps.js");
RequireScript("guitarbattle.js");

var PI = Math.PI;
var evilcackle = LoadSound("KRlaugh.wav");	//the most crucial part of this game!
var window = LoadWindowStyle("reddishfetish.rws");
var window2 = LoadWindowStyle("blueishseethruish.rws");
var fakewindow = LoadImage("jewtheblue.bmp");
var alphagrad = LoadImage("alphagrad.png");
var font = LoadFont("FF6font.rfn");
var font2 = LoadFont("FF6font.rfn");
var pointer = LoadImage("thefinger.png");

var jumpasser = LoadSound("jump_sfx.wav");
var dooropen = LoadSound("door_open_sfx.wav");
var doorclose = LoadSound("door_close_sfx.wav");
var save = LoadSound("save_sfx.wav");
var curse = LoadSound("select_sfx.wav");

var globalcrap = [];

function getter(what)
{
	if (!globalcrap[what])
	{
		var type = what.substr(what.length-3, what.length);
		if (type=="rss") globalcrap[what] = LoadSpriteset(what);
		if (type=="ogg") globalcrap[what] = LoadSound(what);
		if (type=="wav") globalcrap[what] = LoadSound(what);
		if (type=="WAV") globalcrap[what] = LoadSound(what);
		if (type=="mp3") globalcrap[what] = LoadSound(what);
		if (type=="it") globalcrap[what] = LoadSound(what);
		if (type=="mod") globalcrap[what] = LoadSound(what);
		if (type=="png") globalcrap[what] = LoadImage(what);
		if (type=="bmp") globalcrap[what] = LoadImage(what);
		if (type=="gif") globalcrap[what] = LoadImage(what);
	}
	return globalcrap[what];
}

var thePC = new Object();
	thePC.name = "";
var Kbusy = false;
var checkidle = true;
var idle = false;
var idletime = GetTime();
var idledelay = 7000;
var stairdir = 0;
var climbmode = [false, 1];
var conveydir = 0;
var showtheFinger = false;

var talkmode = false;
var talkmarker = 0;
var dontproceed = false;
var nowind = false;
var textdraw = false;
var fading = false;
var dudefading = "";
var fadecol = [0,0,0,255];
var blackout = false;
var theFullText = "";
var theTempText = "";
var justtalked = false;
var subtitleText = false;
var OMGcanIPressEnter = false;
var sec_out_01 = true;		//OMG
var OMGdontNeedToPressEnter = false;
var OMGCANTPressEnter = false;
var maxdelay = false;	//delay before msg auto-closes
var choicetext = [];
var choicetextptr = 0;

var gang = new Object();	//the_enemy
var battlestuff = false;
	//battlestuff = ['setlist01', 'battlebg01.png'...
var eventinprog = false;
var eventmark = 0;
var proceed = true;
var evq = new Array();
var subevq = new Array();
var bevq = new Array();
var beventmark = 0;
var bproceed = true;
var xbo=10, ybo=11;
var xbor=8, ybor=8;
var rfXo=3, rfYo=3;
var winx = xbor;
var winy = ybor;
var renderjunks = [];
var renderjunks_underwin = [];

var dude = new Object();
var the_input_resides_with_this_fellow;
var dont_reattach_input = false;
var thadir = "";		// for reverting dir after talk
var pandir = ["still", 0, 0];
var thecam = 0;
var sec_out_02 = true;
var thempa = "Albrook.rmp";
var thebgm = 0;
var thebgmname = "";
var originaltime = 0;
var timer = new Clock();
var party = new Array();
var evillaughs = 0;
var money = 0;
var saveable = true;
var saveptbounds = false;
var flickering = false;
var dispTownsave = false;
var stage = "Demo";
var dispTown = false;	//town name displays onEnTrance
var people = new Array();	//list of entities
var thecouch = 0;
var COUCH2, COUCH2UP, COUCH3, COUCH3UP, COUCHM;
var thefridge = 0;
var FRIDGE2UP, FRIDGE3, FRIDGEM;
var thetele = 0;
var TELE2, TELE3, TELE3UP, TELEM;
var chests = [];
var switches = [];
var sprintimer = 0;
var pacecounter = 0;
var prevframe = 0;
var sprintmode = 0;	//zero, single, double

var debugable = false;
var boundschecker = false;
var fanceydisplay = true;
var windowstyle = "reddishfetish.rws";
var walking = false;

var key_laugh = KEY_SPACE;
var key_menu = KEY_HOME;
var key_cancel = KEY_BACKSPACE;
var key_debug = KEY_SLASH;
var key_talk = KEY_ENTER;
var key_shoes = KEY_SHIFT;

var fpschoice = 45;

var pcmu = COMMAND_MOVE_NORTH, pcfu = COMMAND_FACE_NORTH;
var pcmr = COMMAND_MOVE_EAST, pcfr = COMMAND_FACE_EAST;
var pcmd = COMMAND_MOVE_SOUTH, pcfd = COMMAND_FACE_SOUTH;
var pcml = COMMAND_MOVE_WEST, pcfl = COMMAND_FACE_WEST;

RequireScript("saveload.js");
RequireScript("battle.js");
RequireScript("miscmap.js");


function game()
{
	if (!IsMapEngineRunning())	//initialize map engine
	{ SetUpdateScript("game();"); thempa="blank.rmp"; MapEngine('blank.rmp', 45); RestartGame();}

	SetRenderScript("rendition();");
	SetUpdateScript("updation();");


		var stretchy = LoadImage("KRTitle_stretchable.bmp");
		var title = LoadAnimation("KRTitle.mng");
		var logo1 = LoadAnimation("DS.mng");
		var logo2 = LoadAnimation("DSflip2.mng");
		var tint = CreateSurface(32,32, CreateColor(0,0,0,75));

		var is_stretched = false;
		var stretch_factor = 0;
		var time01 = GetTime();
		var dontintroaftergame = false;
		var logo2frame = 0;
		var continents = false		//can't use "continue"
		SetFrameRate(30);
		var welcometohell = LoadSound('music/KRintro.it');
		welcometohell.play(false);

		while (GetTime() < time01 + 11000 && !IsKeyPressed(KEY_ENTER))
		{
			if (!is_stretched)
			{
				stretchy.transformBlit(20,124-stretch_factor,  276,124-stretch_factor,  276,124,  20,124);
				stretch_factor++;
				if (stretch_factor == 64) { is_stretched = true; var time04 = GetTime(); SetFrameRate(10);}
			}
			else
			{
				if (GetTime() - time04 > 4000) continents = true;
				
				if (logo2frame < 26 && continents)
				{
					logo2.readNextFrame();
					logo2.drawFrame(128,175);
					logo2frame++;
				}
				else if (continents)
				{
					logo1.readNextFrame();
					logo1.drawFrame(128,175);
					font.setColorMask(CreateColor(0,255,0,128));
					font.drawText(70,210, "KR PROJECT: 2001-2006");
				}
				title.drawFrame(21,61);
				tint.blit(128,175);
				title.readNextFrame();
			//	var time03 = GetTime();
			//	while(time03 + 60 > GetTime())		//fuck you throttle, we got a framerate function!
			//	{
				//	if (IsKeyPressed(KEY_ENTER)) break;
			//	}
			}
			font.setColorMask(White);
			font.drawText(100,125,"PRESS ENTER");
			
			FlipScreen();
		}
		font.setColorMask(White);
		if (IsKeyPressed(KEY_ENTER))
		{
			SetFrameRate(0);
			welcometohell.stop();
			dontintroaftergame = true;
			FlipScreen();
			LoadGameMenu();
		}
		if (!dontintroaftergame)	//prevents intro after engine exit
			eventBox["introscene"].play();
}


function StartGame()
{
	//reset everything in case new game is LOADED
	key_laugh = KEY_SPACE;
	key_menu = KEY_HOME;
	key_cancel = KEY_BACKSPACE;
	key_debug = KEY_SLASH;
	key_talk = KEY_ENTER;
	key_shoes = KEY_SHIFT;
	fpschoice = 45;
	init_charstats();
	//reset all inventory quantities
	money = 0;
	evillaughs = 0;
	thecouch = 0;
	thefridge = 0;
	thetele = 0;
	fanceydisplay = true;
	windowstyle = "reddishfetish.rws";
	window = LoadWindowStyle(windowstyle);
	SetUpdateScript('updation();');
	SetRenderScript('rendition();');
	for (var w=0; w<theHUGEeventlist.length; w++)
	{
		eventBox[theHUGEeventlist[w]].happened = false;
		eventBox[theHUGEeventlist[w]].inprog = false;
	}
	var fools = GetPersonList();
	for (var e=0; e<fools.length; e++)
		DestroyPerson(fools[e]);	//holocate
	if (IsKeyPressed(KEY_SLASH))
	{
		var cheaparray = new Array('prechase', 'scene1', 'gimms_scene', 
		'court_scene', //'exag_scene5',
		'jail_scene', 'chase2', 
		'newhouse', //'furniture_mission', 
		'kefka_dream1', 'morning_after', 'splits', 'k_split_rampage', 'aged_clock', 'vicks_swindle', 'transportmteks', 'splits_closing');
		var pointar = 14;
		while (AreKeysLeft()) GetKey();
		var bitchtime = GetTime();
		while (!IsKeyPressed(key_talk) || GetTime() < bitchtime+100)
		{
			ApplyColorMask(Black);
			font.drawText(20,12, "PreChase");
			font.drawText(20,24, "The Chase");
			font.drawText(20,36, "Gimms");
			font.drawText(20,48, "Court Scenes");
			font.drawText(20,60, "Jail Time");
			font.drawText(20,72, "Chase Again");
			font.drawText(20,84, "Open House");
			font.drawText(20,96, "Nightmares");
			font.drawText(20,108, "Wake up");
			font.drawText(20,120, "Do the Splits");
			font.drawText(20,132, "Kefka's Rampage");
			font.drawText(20,144, "Fix the Clock");
			font.drawText(20,156, "Expensive Fire Rod");
			font.drawText(20,168, "Magitek Highway");
			font.drawText(20,180, "Splits Closing");
			pointer.blit(5, 10+pointar*12);
			FlipScreen();
			switch (GetKey())
			{
			case KEY_UP: pointar--; break;
			case KEY_DOWN: pointar++; break;
			}
		}
		eventBox[cheaparray[pointar]].play();
	}
	else
	eventBox['prechase'].play();
	//eventBox['scene1'].play();
	//eventBox['jail_scene'].play();
	//eventBox['court_scene'].play(true);
	//eventBox['exag_scene3'].play(true);
	//eventBox['flashback_scene'].play(true);
	//eventBox['gimms_scene'].play(true);
	//eventBox['jail_cont_scene'].play();
	//eventBox['furniture_mission'].play();
	//eventBox['splits'].play();
}


function initGame(notime)
{
	AttachInput(thePC.name);
	AttachCamera(thePC.name);
	SetTalkActivationKey(key_talk);
	SetTalkDistance(4);
	SetUpdateScript("updation();");
	SetRenderScript("rendition();");
	eventinprog = false;
	if (!notime) timer.start();
}

function updation()
{
	/* THE KEYS */	
	if (IsKeyPressed(key_laugh) && !eventinprog && !walking) gocrazy();
	if (IsKeyPressed(key_debug) && !eventinprog) warpmap('Debugroom.rmp',50,50,true);
	if (IsKeyPressed(key_menu) && eventsareclean() && !talkmode && !textdraw) mainmenu();
	if (IsKeyPressed(KEY_ESCAPE)) ExitMenu();
	//if (IsKeyPressed(KEY_F3)) Abort(ExecuteZoneScript(0));
	if (IsKeyPressed(KEY_F8)) SetMapEngineFrameRate(GetMapEngineFrameRate()+1);
	if (IsKeyPressed(KEY_F7)) SetMapEngineFrameRate(GetMapEngineFrameRate()-10);
	
	if (IsKeyPressed(KEY_F5) && thempa == "Albrook.rmp" && !IsLayerVisible(4))  NightBrook();

	if (climbmode[0] && !IsInputAttached() && !walking){ tilebasement(); if (dudecanclimb()) SetPersonDirection(thePC.name, 'climb'); }

	if (IsInputAttached() && !Kbusy && eventsareclean())
	{
		var inp = GetInputPerson();
		var spd = GetPersonSpeedX(inp);
	// SPRINT SHOES! //
		if (IsKeyPressed(key_shoes) && !climbmode[0] && MovingKeys() && !dudeobstructed(inp) && !eventBox['scene1'].inprog)
		{
			if (!sprintimer) sprintimer = GetTime();
			if (sprintimer + 1500 < GetTime()) { if (sprintmode!=2)  {sprintmode=2; SetPersonSpeed(inp, 4);} }
			else { if (sprintmode != 1){ sprintmode=1; SetPersonSpeed(inp, 2); }}
		// SPEED PACING
			if ( (spd != 1) && ((pacecounter+1) % ((spd==4) ? 2 : 4) == 0) )	//check every 4 frames for 2x speed; every 2 frames for 4x
			{
				if (GetPersonFrame(inp) == prevframe)		//else sphere changed the frame already
				{
					if (prevframe==3) prevframe=0;
					else prevframe++;
					SetPersonFrame(inp, prevframe);
				}
			}	
			pacecounter++;
			if (pacecounter==15) pacecounter=0;
					prevframe = GetPersonFrame(inp);
		}
		else
		{
			if (spd==4 || spd==2 || spd !=1) 	//hehe.. redundant
				{SetPersonSpeed(inp, 1); sprintmode = 0;}
			sprintimer = false;
			pacecounter = 0;
		}
			
	
	// ANTI-OBSTRUCT SHOES! //
		if (IsKeyPressed(KEY_CTRL))
			IgnoreTileObstructions(inp, true);
		else if (IsIgnoringTileObstructions(inp))
			IgnoreTileObstructions(inp, false);

		if (!walking) tilebasement();
		if (conveydir) conveymode();

		if (IsAnyKeyPressed()){idle = false; idletime = GetTime(); }
		else if (!walking && !idle) SetPersonFrame(thePC.name, 0);
		
	// HUH? FINGER! //
		if (IsKeyPressed(KEY_1) && !showtheFinger)
		{ showtheFinger = true; SetDelayScript(50, 'showtheFinger=false;'); }
	}
	
	if (saveptbounds)
		if (GetPersonX(thePC.name) != saveptbounds[0] || GetPersonY(thePC.name) != saveptbounds[1])
			{saveable = false; saveptbounds = false; flickering=false;}
	
	if (pandir[0] != "still")
		panhandle();
		
	/*EVENTS & SUBS*/
	if (eventsareclean()) NPCjunk();
	else if (proceed) eteq();
	
	for (var a=0; a<subevq.length; a++)
	{
		if (subevq[a].inprog && subevq[a].proceed)
		{
			subevq[a].pos++;
			eval(subevq[a].evq[subevq[a].pos]);
		}
	}
	
	/* TALKING (TEXT) EFFECT */
	if (talkmode) thetalkingstuff()
	
	/* IDLE CHECKING */
	if (checkidle && IsInputAttached() && !walking && idletime + idledelay <= GetTime() && eventsareclean() && !climbmode[0])
	{
		idle = true;
		checkidle = false;
		if (thePC.rss == "Kefka.rss")
			idlefunK(Math.floor(Math.random()*8)); //integer < 8
		else if (thePC.rss == "Brown.rss")
			idlefunB(Math.floor(Math.random()*2));
	}

}

function MovingKeys()
{
	if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_RIGHT))
	return true;
	else return false;
}


function eteq()
{
	if (eventmark >= evq.length)
	{
		evq = new Array();
		eventmark = 0;
		eventinprog = false;
	}
	else
	{
		eval(evq[eventmark]);
		eventmark++;
	}
}

function dudeobstructed(nerd)
{
	var thedir = GetPersonDirection(nerd);
	var thevel = GetPersonSpeedX(nerd);
	if (thedir == "north")
	{
		if (IsPersonObstructed(nerd, GetPersonX(nerd), GetPersonY(nerd)-thevel))
			return true;
	}
	else if (thedir == "south")
	{
		if (IsPersonObstructed(nerd, GetPersonX(nerd), GetPersonY(nerd)+thevel))
			return true;
	}
	else if (thedir == "east")
	{
		if (IsPersonObstructed(nerd, GetPersonX(nerd)+thevel, GetPersonY(nerd)))
			return true;
	}
	else if (thedir == "west")
	{
		if (IsPersonObstructed(nerd, GetPersonX(nerd)-thevel, GetPersonY(nerd)))
			return true;
	}
	return false;
}

function NPCjunk()
{
	if (thePC.name=="") return;	//eventinprog but not *really* eventinprog
	var kpx = GetPersonX(thePC.name);
	var kpy = GetPersonY(thePC.name);
	if (people)
	{
	for (var p=0; p<people.length; p++)
	{
		if (people[p] != thePC.name && people[p] != dude.name)
		{	/* If the npc is near pc... otherwise avoid all this*/
		if ((Math.abs(kpx - GetPersonX(people[p])) <= 20) && (Math.abs(kpy - GetPersonY(people[p])) <= 20))
		{
			if (boundschecker) boundschecker = "font.drawText(1,26,'" + people[p] + "'); font.drawText(1,37,'" + GetPersonX(people[p]) + "," + GetPersonY(people[p]) + "'); font.drawText(1,48,'" + kpx + "," + kpy + "');";
			if (dudeobstructed(people[p])) QueuePersonCommand(people[p], COMMAND_WAIT, false);
		}
		}
		else if (people[p]==dude.name && !dude.toobusy)// && (talkmode || textdraw))
			QueuePersonCommand(dude.name, COMMAND_WAIT, false);
		} //end people check
	}
}

function tilebasement()
{			//difference of where you are and where you should be
	var SPX = GetPersonSpeedX(thePC.name);
	var SPY = GetPersonSpeedY(thePC.name);
	var difX = (GetPersonX(thePC.name) - (Math.floor((GetPersonX(thePC.name)-7)/8)*8+7))/SPX;
	var difY = (GetPersonY(thePC.name) - (Math.floor((GetPersonY(thePC.name)-7) /8)*8+7))/SPY;
	var fullstep = (stairdir) ? 8 : 0;
	if (IsKeyPressed(KEY_UP))
	{	conveymode(1);
		walking=true; DetachInput(); if (!climbmode[0]) SetPersonDirection(thePC.name, "north");
											else {if (dudecanclimb()) SetPersonDirection(thePC.name, 'climb'); SetPersonLayer(thePC.name, climbmode[1]);}
		for (var w=0; w<(8+fullstep)/SPY+difY; w++)
		{	if (!climbmode[0]){
			if (stairdir==1){ SetPersonDirection(thePC.name, "east"); QueuePersonCommand(thePC.name, COMMAND_MOVE_EAST, true);}
			else if (stairdir==2){ SetPersonDirection(thePC.name, "west"); QueuePersonCommand(thePC.name, COMMAND_MOVE_WEST, true);}
			else if (IsKeyPressed(KEY_RIGHT)) QueuePersonCommand(thePC.name, COMMAND_MOVE_EAST, true);
			else if (IsKeyPressed(KEY_LEFT)) QueuePersonCommand(thePC.name, COMMAND_MOVE_WEST, true);}
			QueuePersonCommand(thePC.name, COMMAND_MOVE_NORTH, false); 
		}
		QueuePersonScript(thePC.name, "trigtest(); if (!Kbusy && !climbmode[0]) AttachInput(thePC.name); walking=false; ", false);
	}
	else if (IsKeyPressed(KEY_DOWN))
	{	conveymode(2);
		walking=true; DetachInput(); if (!climbmode[0]) SetPersonDirection(thePC.name, "south");
											else {if (thePC.name=='Kefka') SetPersonDirection(thePC.name, 'climb'); SetPersonLayer(thePC.name, climbmode[1]);}
		for (var w=0; w<(8+fullstep)/SPY-difY; w++)
		{	if (!climbmode[0]){
			if (stairdir==1){ SetPersonDirection(thePC.name, "west"); QueuePersonCommand(thePC.name, COMMAND_MOVE_WEST, true);}
			else if (stairdir==2){ SetPersonDirection(thePC.name, "east"); QueuePersonCommand(thePC.name, COMMAND_MOVE_EAST, true);}
			else if (IsKeyPressed(KEY_RIGHT)) QueuePersonCommand(thePC.name, COMMAND_MOVE_EAST, true);
			else if (IsKeyPressed(KEY_LEFT)) QueuePersonCommand(thePC.name, COMMAND_MOVE_WEST, true);}
			QueuePersonCommand(thePC.name, COMMAND_MOVE_SOUTH, false);
		}
		QueuePersonScript(thePC.name, "trigtest(); if (!Kbusy && !climbmode[0]) AttachInput(thePC.name); walking=false;", false);
	}
	else if (IsKeyPressed(KEY_RIGHT) && !climbmode[0])
	{	conveymode(4);
		walking=true; DetachInput(); SetPersonDirection(thePC.name, "east");
		for (var w=0; w<8/SPX-difX; w++)
		{
				if (stairdir==1) QueuePersonCommand(thePC.name, COMMAND_MOVE_NORTH, true);
			 else if (stairdir==2) QueuePersonCommand(thePC.name, COMMAND_MOVE_SOUTH, true);
			QueuePersonCommand(thePC.name,COMMAND_MOVE_EAST,false);
		}
		QueuePersonScript(thePC.name, "if (!Kbusy) AttachInput(thePC.name); walking=false;", false);
		QueuePersonScript(thePC.name, "if (climbmode[0]) DetachInput();", false);
	}
	else if (IsKeyPressed(KEY_LEFT) && !climbmode[0])
	{ conveymode(3);
		walking=true; DetachInput(); SetPersonDirection(thePC.name, "west");
		for (var w=0; w<8/SPX+difX; w++)
		{
				if (stairdir==1) QueuePersonCommand(thePC.name, COMMAND_MOVE_SOUTH, true);
			 else if (stairdir==2) QueuePersonCommand(thePC.name, COMMAND_MOVE_NORTH, true);
			QueuePersonCommand(thePC.name,COMMAND_MOVE_WEST,false);
		}
		QueuePersonScript(thePC.name, "trigtest(); if (climbmode[0]) anothereight(); else {if (!Kbusy) AttachInput(thePC.name); walking=false;}", false);
	}
	

	
	if (IsKeyPressed(KEY_F6))
	{
		SetPersonX(thePC.name, GetPersonX(thePC.name)+difX);
		SetPersonY(thePC.name, GetPersonY(thePC.name)+difY);
		stairdir = 0;
		walking = false;
	}
}


function trigtest()
{
	if (IsTriggerAt(GetPersonX(thePC.name), GetPersonY(thePC.name), GetPersonLayer(thePC.name)) && (climbmode[0] || GetPersonDirection(thePC.name)=='west'))	//might not need additional conditional
		ExecuteTrigger(GetPersonX(thePC.name),GetPersonY(thePC.name),GetPersonLayer(thePC.name));
}
	
function anothereight()
{
	if (climbmode[0] && GetPersonDirection(thePC.name)=="west" && walking)		//moves another 8 pix to hit ladder
	{
		for (var w=0; w<8/GetPersonSpeedX(thePC.name); w++)
			QueuePersonCommand(thePC.name,COMMAND_MOVE_WEST,false);
		QueuePersonScript(thePC.name, "walking=false;", false);
	}
}

function dudecanclimb()
{	//more sprites in future
	if (charstats[party[0]].rss=='Kefka.rss' || charstats[party[0]].rss=='Brown.rss') return true;
	else return false;
}

function conveymode(dir)
{
	if (!conveydir){
		if (sprintmode==0) SetPersonSpeed(thePC.name, 1);
		else if (sprintmode==1) SetPersonSpeed(thePC.name, 2);
		else if (sprintmode==2) SetPersonSpeed(thePC.name, 4);
		return;
	}
	if (!dir)	//not walking
	{
		if (sprintmode==0) SetPersonSpeed(thePC.name, 1);
		if (conveydir==1)
		{
			QueuePersonCommand(thePC.name, pcmu, true);
		}
		else if (conveydir==2)
		{
			QueuePersonCommand(thePC.name, pcmd, true);
		}
		else if (conveydir==3)
		{
			QueuePersonCommand(thePC.name, pcml, true);
		}
		else if (conveydir==4)
		{
			QueuePersonCommand(thePC.name, pcmr, true);
		}
	}
	else
	{
		if (conveydir==1)
		{
			if (dir==1) { 
				if (sprintmode==0) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 2);
				else if (sprintmode==1) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 4);
				else if (sprintmode==2) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 6);
			}
			else if (dir==2) { 
				if (sprintmode==0) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), .5);
				else if (sprintmode==1) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 1);
				else if (sprintmode==2) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 2);
			}
			else QueuePersonCommand(thePC.name, pcmu, true);
		}
		else if (conveydir==2)
		{
			if (dir==2) { 
				if (sprintmode==0) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 2);
				else if (sprintmode==1) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 4);
				else if (sprintmode==2) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 6);
			}
			else if (dir==1) { 
				if (sprintmode==0) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), .5);
				else if (sprintmode==1) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 1);
				else if (sprintmode==2) SetPersonSpeedXY(thePC.name, GetPersonSpeedX(thePC.name), 2);
			}
			else QueuePersonCommand(thePC.name, pcmd, true);
		}
		else if (conveydir==3)
		{
			if (dir==3) { 
				if (sprintmode==0) SetPersonSpeedXY(thePC.name, 2, GetPersonSpeedY(thePC.name));
				else if (sprintmode==1) SetPersonSpeedXY(thePC.name, 4, GetPersonSpeedY(thePC.name));
				else if (sprintmode==2) SetPersonSpeedXY(thePC.name, 6, GetPersonSpeedY(thePC.name));
			}
			else if (dir==4) { 
				if (sprintmode==0) SetPersonSpeedXY(thePC.name, .5, GetPersonSpeedY(thePC.name));
				else if (sprintmode==1) SetPersonSpeedXY(thePC.name, 1, GetPersonSpeedY(thePC.name));
				else if (sprintmode==2) SetPersonSpeedXY(thePC.name, 2, GetPersonSpeedY(thePC.name));
			}
			else QueuePersonCommand(thePC.name, pcml, true);
		}
		else if (conveydir==4)
		{
			if (dir==4) { 
				if (sprintmode==0) SetPersonSpeedXY(thePC.name, 2, GetPersonSpeedY(thePC.name));
				else if (sprintmode==1) SetPersonSpeedXY(thePC.name, 4, GetPersonSpeedY(thePC.name));
				else if (sprintmode==2) SetPersonSpeedXY(thePC.name, 6, GetPersonSpeedY(thePC.name));
			}
			else if (dir==3) { 
				if (sprintmode==0) SetPersonSpeedXY(thePC.name, .5, GetPersonSpeedY(thePC.name));
				else if (sprintmode==1) SetPersonSpeedXY(thePC.name, 1, GetPersonSpeedY(thePC.name));
				else if (sprintmode==2) SetPersonSpeedXY(thePC.name, 2, GetPersonSpeedY(thePC.name));
			}
			else QueuePersonCommand(thePC.name, pcmr, true);
		}
	}
}



function talkjunk(junk)
{
	if (!justtalked)
	{
		if (!talkmode)
		{
			for (var a=0; a<subevq.length; a++) if (subevq[a].inprog) subevq[a].proceed = false;
			if (eventsareclean() && !gang.pause4event)
			{
				if (!dude.name){ dude = eval(GetCurrentPerson());
				thadir = GetPersonDirection(dude.name);}
				SetPersonFrame(thePC.name,0);	//no mid-stride!
				if (dude.choice)
				{
					for (var i=0; i<3; i++)
						choicetext[i] = eval("dude.choice" + (i+1));
				}
				if (dude.face)
				{
					var kdir = GetPersonDirection(thePC.name);
					if (kdir == "north") SetPersonDirection(dude.name, "south");
					if (kdir == "south") SetPersonDirection(dude.name, "north");
					if (kdir == "east") SetPersonDirection(dude.name, "west");
					if (kdir == "west") SetPersonDirection(dude.name, "east");
					SetPersonFrame(dude.name, 0);
				}
				if (junk != undefined)	theFullText = junk;
				else
				{
					if (dude.incremental)
					{
						theFullText = eval(dude.name+".talk"+dude.diamark);
						if (dude.firstonce){ if (dude.diamark==1) dude.diamark++;}
						else dude.diamark++;
						if (!eval(dude.name + ".talk" + dude.diamark)) dude.diamark = 1;
						if (dude.talkrand)
						{		//get number of talks
							for (ghey=1; true; ghey++) if (!eval(dude.name + ".talk" + ghey)) break;
							theFullText = eval(dude.name+".talk"+ (Math.floor(Math.random()*(ghey-1))+1));
						}
					}
					else theFullText = eval(dude.name +".talk"+dude.diamark);
				}
				Kbusy = true;	
				if (!OMGdontNeedToPressEnter && !maxdelay) DetachInput();
			}
			else theFullText = junk;
			if (eventinprog) proceed = false;
			if (gang.pause4event) bproceed = false;
			talkmode = true;
			textdraw = true;
			checkidle = false;
		}
	}
}

function subtitle(eng)
{
	if (!justtalked) subtitleText = eng;
}

function thetalkingstuff()
{		
	if (talkmarker >= theFullText.length)	//when full message is displayed..
	{
		//only do this once
		if (sec_out_01)
		{	sec_out_01 = false;
			SetDelayScript( ((eventinprog) ? 13 : 8), "OMGcanIPressEnter = true;");
			if (maxdelay){ OMGdontNeedToPressEnter=false; SetDelayScript(maxdelay, "if (!sec_out_01){OMGdontNeedToPressEnter = true; OMGCANTPressEnter=false;}");}
		}

		if (AreKeysLeft() && dude.choice)
		{
			var OMG = GetKey();
			if (OMG==KEY_UP){ if (dude.choice3) {if (dude.pointah>0) dude.pointah--;} else dude.pointah=0;}
			if (OMG==KEY_DOWN){ if (dude.choice3) {if (dude.pointah<2) dude.pointah++;} else dude.pointah=1;}
		}
		if (AreKeysLeft() && choicetext)
		{
			var OMFG = GetKey();
			if (OMFG==KEY_UP){ if (choicetext[2]) { if (choicetextptr>0) choicetextptr--;} else choicetextptr=0;}
			if (OMFG==KEY_DOWN){ if (choicetext[2]){ if (choicetextptr<2) choicetextptr++;} else choicetextptr=1;}
		}
		if (OMGcanIPressEnter && !OMGCANTPressEnter && (IsKeyPressed(key_talk) || OMGdontNeedToPressEnter)) 
		{			//reset everything!
			justtalked = true;	//is actually set earlier for safety, but this is the original
			talkmarker=0;
			theFullText = "";
			theTempText = "";
			subtitleText = false;
			talkmode = false;
			textdraw = false;
			Kbusy = false;
			OMGcanIPressEnter = false;
			OMGdontNeedToPressEnter = false;
			sec_out_01 = true;
			if (eventsareclean() && !gang.pause4event)
			{
				
				if (!dude.incremental && eval(dude.name + ".talk" + ++dude.diamark))	//if has more to say..
				{
					theFullText = eval(dude.name+".talk"+dude.diamark);
					SetDelayScript(10, "talkmode = true; textdraw = true;");
				}
				else
				{
					while (AreKeysLeft()) GetKey();
					if (dude.choice)
					{
						justtalked = false;
						talkmode = false;
						//dude.choice = false;
						if (dude.pointah){	if (dude.no)	dude.no();	else resetTalk(); }
						else 			{	if (dude.yes)	dude.yes();	}
					}
					else
					{
						if (dude.shop) dude.shop();
						SetDelayScript(1, "justtalked = false;");
						if (!dude.toobusy) SetPersonDirection(dude.name, thadir);
						if (!dude.incremental) dude.diamark = 1;
						dude = new Object();
						if (!OMGdontNeedToPressEnter) AttachInput(thePC.name);
					}
				}
				checkidle = true;
				idletime = GetTime();
			}
			else
			{
				justtalked = false;
				if (!dontproceed) proceed = true;		//intro,subevents
				dontproceed = false;
				for (var a=0; a<subevq.length; a++) if (subevq[a].inprog) subevq[a].proceed = true;
				if (gang.pause4event) bproceed = true;
				dude = new Object();
				choicetext = [];
			}
		}
	}
	else	//continue [typing] the dialogue
	{
		if (theFullText.substr(talkmarker, 2) == "/w")		//CHECK FOR HESITATION
		{
			talkmode = false;
			justtalked = true; 	//so they can't retalk during hesitation
			SetDelayScript(10, "talkmode = true;"); // justtalked=false;"); this just messes junk up!
			talkmarker+=2;
		}
		else
		{
			theTempText += theFullText.charAt(talkmarker);
			talkmarker++;
			if (talkmarker > 15 && IsKeyPressed(key_talk) && !OMGCANTPressEnter)	//if you don't care
			{
				for (; talkmarker<theFullText.length; talkmarker++)
				{
					if (theFullText.substr(talkmarker, 2) == "/w") talkmarker++
					else theTempText += theFullText.charAt(talkmarker);
				}
			}
		}
	}

}

function talkagain(smack)
{
	theFullText = smack;
	justtalked = true;
	dude.choice = false;
	choicetext = [];
	SetDelayScript(5, "talkmode = true; textdraw = true;");
}

function resetTalk()
{
	if (dude.shop) dude.shop();
	SetDelayScript(1, "justtalked = false;");
	if (!dude.toobusy) SetPersonDirection(dude.name, thadir);
	if (!dude.incremental) dude.diamark = 1;
	dude = new Object();
	choicetext = [];
	if (!OMGdontNeedToPressEnter && !dont_reattach_input) AttachInput(thePC.name);
	dont_rettach_input = false; 	//resetter	(used in jail thinger)
}

function eventsareclean()
{
	for (var a=0; a<subevq.length; a++) if (subevq[a].inprog && !subevq[a].clean) return false;	//clean: subevents that dont mess with sheeit
	if (eventinprog) { if (eventinprog=="pause") return true; else return false; }
	return true;
}

function maineventsareclean()
{
	if (eventinprog) { if (eventinprog=="pause") return true; else return false; }
	return true;
}

function rendition()
{
	//shut rendered under windows (sewer boss)
	for (var f=0;(renderjunks_underwin[f]); f++)
		 eval(renderjunks_underwin[f]);
		 
	if (dispTown)
	{
		dispTownsave = dispTown;
		if (!eventinprog || eventinprog=='pause' || gang.fighting)
		{
		window.drawWindow(GetScreenWidth()/2-font.getStringWidth(dispTown)/2-15,ybo, font.getStringWidth(dispTown)+30, 10);
			
			alphagrad.transformBlit
			(
				GetScreenWidth()/2 -font.getStringWidth(dispTown)/2 -15 -rfXo,	ybo-rfYo,
				GetScreenWidth()/2+font.getStringWidth(dispTown)/2+15+rfXo,	ybo-rfYo,
				GetScreenWidth()/2+font.getStringWidth(dispTown)/2+15+rfXo,	ybo+10+rfYo,
				GetScreenWidth()/2 -font.getStringWidth(dispTown)/2 -15 -rfXo,	ybo+10+rfYo
			);
		
		font.drawText(GetScreenWidth()/2 - font.getStringWidth(dispTown)/2, ybo, dispTown);
		SetDelayScript(150, "dispTown = false;");
		}
		else dispTown = false;
	}
	
	if (fading)
	{
		blackout = false;
		if (fading == "in")
		{
			fadecol[3] -= 25;
			ApplyColorMask(CreateColor(fadecol[0],fadecol[1],fadecol[2],fadecol[3]));
			if (fadecol[3] < 10)
			{
				fading = false;
				if (!dontproceed) proceed = true;	//don't mess things up when event paused
				for (var a=0; a<subevq.length; a++) if (subevq[a].inprog && !subevq[a].nofadepro) subevq[a].proceed = true;
				dontproceed=false;
				fadecol[3] = 0;
			}
		}
		else if (fading == "out")
		{
			fadecol[3] += 25;
			ApplyColorMask(CreateColor(fadecol[0],fadecol[1],fadecol[2],fadecol[3]));
			if (fadecol[3] > 255)
			{
				fading = false;
				if (!dontproceed) proceed = true;
				for (var a=0; a<subevq.length; a++) if (subevq[a].inprog && !subevq[a].nofadepro) subevq[a].proceed = true;
				dontproceed=false;
				fadecol[3] = 255;
			}
		}
		else if (fading == "person_out")
		{
			fadecol[3] -= 5;
			SetPersonMask(dudefading, CreateColor(255,255,255,fadecol[3]));
			if (fadecol[3] < 10)
			{
				fading = false;
				DestroyPerson(dudefading);
				people = GetPersonList();
				if (!dontproceed) proceed = true;
				for (var a=0; a<subevq.length; a++) if (subevq[a].inprog) subevq[a].proceed = true;
				fadecol[3] = 0;
			}
		}
	}
	if (boundschecker) eval(boundschecker);	//gay
	if (blackout) ApplyColorMask(CreateColor(fadecol[0], fadecol[1], fadecol[2], fadecol[3]));
	if (showtheFinger) pointer.rotateBlit(map2screenX(GetPersonX(thePC.name)-25), map2screenY(GetPersonY(thePC.name)-30), (Math.PI/180)*45);

	if (textdraw)
	{
		if (nowind)
		font.drawTextBox(winx,winy,305,190,0, theTempText);
		else
		{
			if (textdraw=="smalligan")
			{
				window.drawWindow(50, 30, 220, 20);
				font.drawText(160-font.getStringWidth(theFullText)/2, 35, theTempText);
			}
			else
			{
				//fakewindow.blit(1,winy-ybo);
				window.drawWindow(xbor,winy, 320-xbor*2, 60); 
				alphagrad.transformBlit(xbor-rfXo, 	 	winy-(ybor-rfYo),  
											320-(xbor-rfXo), winy-(ybor-rfYo),  
											320-(xbor-rfXo), winy+60+rfYo,  
											xbor-rfXo, 		 winy+60+rfYo
								);
				font.drawTextBox(winx+2, winy, 310, 60, 0, theTempText);
			}
		}			
		if (subtitleText && talkmarker >= 10) font.drawText(7,227, subtitleText);
		if ((choicetext[0] || dude.choice) && talkmarker >= theFullText.length)
		{
			if (dude.choice)
			{
			font.drawText(20, winy+15+dude.YN_offset*15, (dude.choice1) ? dude.choice1 : "YES");
			font.drawText(20, winy+30+dude.YN_offset*15, (dude.choice2) ? dude.choice2: "NO");
			font.drawText(20, winy+45+dude.YN_offset*15, (dude.choice3) ? dude.choice3 : "");	//3's the limit for now
			pointer.blit(5, winy+15+dude.YN_offset*15+dude.pointah*15);
			}
			else
			{
			font.drawText(20, winy+15, choicetext[0]);
			font.drawText(20, winy+30, choicetext[1]);
			font.drawText(20, winy+45, choicetext[2]);
			pointer.blit(5, winy+15+choicetextptr*15);			
			}
		}
	}
	for (var f=0;(renderjunks[f]);f++)
		 eval(renderjunks[f]);

	//font.drawText(5,220, eventmark +" : "+ eventinprog +" : "+  eventsareclean());
	//font.drawText(5,230, evq[eventmark]);
	//font.drawText(5, 220, GetPersonLayer(thePC.name));
	//font.drawText(5, 230, climbmode);
	//font.drawTextBox(0,0,320,240,10,subevq.length);
	//font.drawText(15,160, justtalked); //thempa);
	//font.drawText(1,15,timer.getTime()); //debug feature
	// save for debug junk
	//if (thePC.name){
	//font.drawText(215,200, GetPersonX(thePC.name) + " , " + GetPersonY(thePC.name));
	//font.drawText(215,210, (GetPersonX(thePC.name)-7)/16 + " , " + (GetPersonY(thePC.name)-7)/16);
	//font.drawText(215,220, (GetPersonX(thePC.name) + Math.floor((GetPersonX(thePC.name)-7)/8)*8+7 - GetPersonX(thePC.name)) +" , " + (GetPersonY(thePC.name) + Math.floor((GetPersonY(thePC.name)-7)/8)*8+7 - GetPersonY(thePC.name)) );
	//}
	//font.drawText(5,220,subevq);//thempa + ", " + thebgmname + ", " + timewarping.proceed);//GetMapEngineFrameRate());
}


function retard(eman,rss,sheeit)
{
	this.name = eman;
	this.step = 0;
	this.mark = 0;
	this.rss = rss;
	this.dirs = new Array('w');
	CreatePerson(eman, rss, sheeit);
}

function npcrap(eman)
{
	// GetCurrentPerson() should== eman
	this.name = eman;
	this.step = 0;
	this.mark = 0;
	this.face = true;
	this.diamark = 1;
	/* unneeded space consumption
	this.toobusy = false;
	this.ignoreobs = false;
	this.shop = false;
	this.incremental = false;
	this.talkrand = false;
	this.firstonce = false;
	this.notperson = false;
	this.choice = false;
	this.choice1 = false;
	this.choice2 = false;
	*/
	this.YN_offset = 0;
	this.pointah = 0;
	this.ile = undefined;
	this.dirs = new Array('w');
}

function isinsubq(it)
{
	for (var b=0; b<subevq.length; b++)
		if (subevq[b] == it) return true;
	return false;
}

function remove(it)
{
	var withoutit = new Array();
	for (var b=0; b<subevq.length; b++)
		if (subevq[b] != it) withoutit[withoutit.length] = subevq[b];
	subevq = withoutit;		
}


function flicker()
{
	if (flickering) return;
	flickering = true;		//reset when PC moves (saveptbounds)
	fadecol = [255,0,0,100];
	blackout = true;
	saveable = true;
	saveptbounds = [GetPersonX(thePC.name), GetPersonY(thePC.name)];
	SetDelayScript(1, "fadecol = [255,0,0,110];");
	SetDelayScript(2, "fadecol = [255,0,0,120];");
	SetDelayScript(3, "fadecol = [255,0,0,130];");
	SetDelayScript(4, "fadecol = [255,0,0,140];");
	SetDelayScript(5, "fadecol = [255,0,0,150];");
	SetDelayScript(6, "fadecol = [255,0,0,160];");
	SetDelayScript(7, "fadecol = [255,0,0,170];");
	SetDelayScript(8, "fadecol = [255,0,0,170];");
	SetDelayScript(9, "fadecol = [255,0,0,170];");
	SetDelayScript(10, "fadecol = [255,0,0,180];");
	SetDelayScript(11, "fadecol = [255,0,0,190];");
	SetDelayScript(12, "fadecol = [255,0,0,200];");
	SetDelayScript(13, "fadecol = [255,0,0,190];");
	SetDelayScript(14, "fadecol = [255,0,0,180];");
	SetDelayScript(15, "fadecol = [255,0,0,170];");
	SetDelayScript(16, "fadecol = [255,0,0,160];");
	SetDelayScript(17, "fadecol = [255,0,0,150];");
	SetDelayScript(18, "fadecol = [255,0,0,140];");
	SetDelayScript(19, "fadecol = [255,0,0,130];");
	SetDelayScript(20, "blackout=false; fadecol = [0,0,0,0];");
}


/* MISC MAP SCRIPTS */

function AttachInput2(foo)
{
	if (foo) the_input_resides_with_this_fellow = foo;
	else the_intput_resides_with_this_fellow = thePC.name;
}

function DetachInput2()
{
	the_input_resides_with_this_fellow = undefined;
}

function warpmap(map,xset,yset,keyed)
{
	if (eventBox['scene1'].inprog) return;	//stay outside, ya bum!
	if (eventsareclean())
	{
		timewarping = new miniscene();
		idle = false;
		if (map == undefined) map = thempa;
		if (dispTown) dispTown = false;
	//	DetachInput();
		DetachCamera();
		ClearPersonCommands(thePC.name);
		if (GetPersonSpeedX(thePC.name)==4) SetPersonSpeed(thePC.name, 2);
		if (!keyed)
		{
			if (GetPersonDirection(thePC.name)=="north")
				for (var f=0; f<13; f++)
				QueuePersonCommand(thePC.name, pcmu, false);
			else if (GetPersonDirection(thePC.name)=="south")
				for (var f=0; f<13; f++)
				QueuePersonCommand(thePC.name, pcmd, false);
		}
		
		timewarping.ateq("");
		if (eventBox['kefka_split'].inprog && thempa=="Tzen.rmp" && eventBox['tzen_welcome'].happened && !farmandog.happened)
			if (GetPersonDirection('tzeng')=="owned") 
				timewarping.ateq("farmandog.play('"+map+"',"+xset+","+yset+");");
				
		if (eventBox['vicks_split'].inprog && thempa=="Albrook.rmp" && map=="Trishop.rmp" && !pushmtek.happened)
			timewarping.ateq("pushmtek.play();");
				
				
							//fade out current bgm same time as screenfade, fadeOutScreen pauses timewarp
							//whichever finishes first executes blackout and pauses again
							//whichever finishes second proceeds timewarp (not precedes)
		var sameshit = (getmapbgm(map)==thebgmname);
		if (thebgm!=0 && !sameshit)
			if (maineventsareclean())
				timewarping.ateq("bgmswitch(getmapbgm('" + map + "'), false, !getmapbgmfadeIN('" + map + "'));");
				
		timewarping.ateq("dontproceed=true; fadeOutScreen();");
		timewarping.ateq("blackout = true;" + ((!sameshit && thebgm!=0) ? " timewarping.proceed=false;" : ""));		
		if (thebgm==0 )//&& !sameshit)		//no need to fade out prev bgm, switch it NOW before map changes and WAIT for it to proceed
		{
			if (maineventsareclean())
			{
				timewarping.proceed=false;
				timewarping.ateq("bgmswitch(getmapbgm('" + map + "'), true, true);");	//dont fade OUT nor IN
			}
		}
		timewarping.ateq("ChangeMap('" + map + "'); thempa = '" + map + "'; people = GetPersonList();");
		timewarping.ateq("AttachCamera(thePC.name);");
		if (xset != undefined)// && yset != undefined)
		{
			if (typeof xset == "number")
				timewarping.ateq("SetPersonX(thePC.name," + xset + "); SetPersonY(thePC.name," + yset + ");");
			else timewarping.ateq("SetPersonX(thePC.name," + xset[0] + "); SetPersonY(thePC.name," + xset[1] + "); SetPersonLayer(thePC.name," + xset[2] +");");
		}
		timewarping.ateq("SetPersonDirection(thePC.name, 'south');");
		timewarping.ateq("dontproceed=true; fadeInScreen(); walking = false; timewarping.inprog = false; idletime = GetTime(); checkidle = true; remove(timewarping);");
		timewarping.inprog = true;
		timewarping.proceed = true;
		subevq.push(timewarping);
	}
}


function gocrazy()
{
	if (!Kbusy && thempa != "Worldmap.rmp" && thePC.rss == "Kefka.rss")
	{
		Kbusy = true;
		idle = false;
		DetachInput();
		SetPersonDirection("Kefka", "laugh");
		SetPersonFrame("Kefka", 0);
		evilcackle.stop();
		evilcackle.play(false);
		evillaughs++;
		for (var f=0; f<17; f++)
		{
			SetDelayScript(5*f, "SetPersonFrame('Kefka', " + ((f%2==0) ? 1 : 0) + ");");
			if (f==2) SetDelayScript(5*f+1, 'InsaneLaughEngine();');
		}
		SetDelayScript(85, "AttachInput('Kefka'); Kbusy = false; idle = true; idletime=GetTime();");
	}
}


function ateq(bagawk)
{
	evq[evq.length] = bagawk;
}

function atbeq(sqwaak)
{
	bevq[bevq.length] = sqwaak;
}

function eprep()
{
	if (!eventBox['scene1'].inprog) eventinprog = false;
	eventmark = 0 ;
	evq = new Array();
	if (isdude(thePC.name)) SetPersonSpeed(thePC.name,1);	//debug event views w/ shoes -> nuts
}



/* TEH MUSIC JUNKS!!1 */

function bgmswitch(ugh, nofadeOUT, nofadeIN, norepeat, yarg)
{
	var blank = false;
	if (ugh == "" || ugh==undefined) blank = true;
	if (ugh != thebgmname)								//not already playing
	{
		var tunes = GetFileList("sounds/music");
		for (var z=0; z<tunes.length; z++)
		{
			if ("music/" + tunes[z] == ugh) break;			//check if it exists
			if (z==tunes.length-1) blank = true;
		}
		
		//fade section
		var framgramit = 0;

		if (!nofadeOUT && thebgm!=0)	//then YES, fade OUT
			for (; framgramit<10; framgramit++)
				SetDelayScript(framgramit*3, "thebgm.setVolume(thebgm.getVolume()-25.5);");
				
		var stringy = "if (thebgm != 0) { thebgm.setVolume(0); thebgm.stop(); }";
		stringy += "if (!"+blank+")";		//now isn't this just lovely <3
		stringy+= "{ ";
		stringy += "	thebgmname = '"+ugh+"'; thebgm = LoadSound(thebgmname);";
		stringy += "	if ("+!nofadeIN+") thebgm.setVolume(0); thebgm.play("+!norepeat+");";
		stringy += "}";
		stringy += "else { thebgm = 0; thebgmname = ''; }";
		SetDelayScript(framgramit*3, stringy);

		//at this point, timewarping is waiting for the bgm fade IN before the map changes
		var tempy = framgramit;
		if (!nofadeIN && !blank)		//then YES, fade IN, my dear brother
			for (++framgramit; framgramit<tempy+10; framgramit++)
				SetDelayScript(framgramit*3, "thebgm.setVolume(thebgm.getVolume()+25.5);");
				
		SetDelayScript(framgramit*3, "if (timewarping.inprog) timewarping.proceed=true;");
	}
	else
		if (timewarping.inprog) timewarping.proceed=true;
}

/*	The Worldmap Updation	*/
function mapdation()
{
}

/*	The Worldmap Rendition	*/
function mapdition()
{
	mutilate = GrabImage(0,0,320,240);
	ApplyColorMask(Black);
	mutilate.transformBlit(100,0,  220,0,  320,240,  0,240);
	assfoo.blit(GetPersonX(thePC.name), GetPersonY(thePC.name));
	GarbageCollect();
}


function panhandle()	//manly!
{
	//	pandir: [dir, dest, spd]
	
	if (pandir[0]=="ul" || pandir[0]=="dr" || pandir[0]=="ur" || pandir[0]=="dl")
	{
		diaghandle();
		return;
	}
	var L = pandir[0]=="left", R = pandir[0]=="right", U = pandir[0]=="up";
	var CX = GetCameraX(), CY = GetCameraY();
	var axis = (L || R);	//0:y|1:x
	var D = pandir[1];

	if ( (axis   ?   (L ? CX <= D : CX >= D)   :   (U ? CY <= D : CY >= D))  )	//at or beyond destination
	{
		pandir = ['still', 0, 0];
		if (!dontproceed) proceed = true;	//keeps main event paused	//proceeds miniscenes
		for (var a=0; a<subevq.length; a++) if (subevq[a].inprog) subevq[a].proceed=true;
		dontproceed=false;
	}
	else
	{
		if (axis)	SetCameraX(CX + (L ? -pandir[2] : pandir[2]));
		else	SetCameraY(CY + (U ? -pandir[2] : pandir[2]));
	}
}

function diaghandle()
{
	// ...this is gunna be nuts!
	// pandir: {(ul - dl - ur - dr), [dest_x, dest_y], [speed_x, speed_y]}
	// whichever destination (x or y) is reached first is the end
	var vec1 = (pandir[0]=="ul" || pandir[0]=="dr");	// like this: "\"
	var vec2 = (pandir[0]=="ur" || pandir[0]=="dl"); // like this: "/"
	var ul = pandir[0]=="ul", dr = pandir[0]=="dr", ur = pandir[0]=="ur", dl = pandir[0]=="dl";
	var CX = GetCameraX(), CY = GetCameraY();
	var dx = pandir[1][0], dy = pandir[1][1];
	
	if ( (vec1   ?   (ul ? (CX <= dx || CY <= dy) : (CX >= dx || CY >= dy))
			    :   (ur ? (CX >= dx || CY <= dy) : (CX <= dx || CY >= dy))) )
	{
		pandir = ['still', 0, 0];
		if (!dontproceed) proceed = true;	//keeps main event paused	//proceeds miniscenes
		for (var a=0; a<subevq.length; a++) if (subevq[a].inprog) subevq[a].proceed=true;
		dontproceed=false;
	}
	else
	{
		if (vec1)
		{ SetCameraX(CX + (ul ? -pandir[2][0] : pandir[2][0])); SetCameraY(CY + (ul ? -pandir[2][1] : pandir[2][1])); }
		else
		{ SetCameraX(CX + (ur ? pandir[2][0] : -pandir[2][0])); SetCameraY(CY + (ur ? -pandir[2][1] : pandir[2][1])); }
	}
	
	
}

function camerapanx(dest_x,speed)
{
	pandir = [(GetCameraX() < dest_x) ? "right" : "left", dest_x, speed];
}

function camerapany(dest_y,speed)
{
	pandir = [(GetCameraY() < dest_y) ? "down" : "up", dest_y, speed];
}

function camerapand(dest_x,dest_y,speed)
{
	var bearing;
	       if (GetCameraX() > dest_x && GetCameraY() > dest_y) bearing = "ul";
	else if (GetCameraX() > dest_x && GetCameraY() < dest_y) bearing = "dl";
	else if (GetCameraX() < dest_x && GetCameraY() > dest_y) bearing = "ur";
	else if (GetCameraX() < dest_x && GetCameraY() < dest_y) bearing = "dr";

	pandir = [bearing, [dest_x, dest_y], (typeof speed == "number") ? [speed,speed] : speed];
}


function fadeOutScreen()
{
	proceed = false;
	for (var a=0; a<subevq.length; a++) if (subevq[a].inprog) subevq[a].proceed = false;
	fading = "out";
	if (eventsareclean()) fadecol = [0,0,0,0];
	fadecol[3] = 0;
}

function fadeInScreen()
{
	proceed = false;
	for (var a=0; a<subevq.length; a++) if (subevq[a].inprog) subevq[a].proceed = false;
	fading = "in";
	if (eventsareclean()) fadecol = [0,0,0,255];
	fadecol[3] = 255;
}


function fadePersonOut(dude)
{
	proceed = false;
	for (var a=0; a<subevq.length; a++) if (subevq[a].inprog) subevq[a].proceed = false;
	fading = "person_out";
	dudefading = dude;
}


function walkgay(guy, dir)
{
	
	switch (dir)
	{		
		case ("l"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_WEST, false);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("r"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_EAST, false);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("u"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_NORTH, false);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("d"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_SOUTH, false);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("w"):
		{
			QueuePersonCommand(guy.name, COMMAND_WAIT, false);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("fu"):
		{
			QueuePersonCommand(guy.name, COMMAND_FACE_NORTH, false);
			guy.step++; guy.mark = 0;
			break;
		}
		case ("fl"):
		{
			QueuePersonCommand(guy.name, COMMAND_FACE_WEST, false);
			guy.step++; guy.mark = 0;
			break;
		}
		case ("fr"):
		{
			QueuePersonCommand(guy.name, COMMAND_FACE_EAST, false);
			guy.step++; guy.mark = 0;
			break;
		}
		case ("fd"):
		{
			QueuePersonCommand(guy.name, COMMAND_FACE_SOUTH, false);
			guy.step++; guy.mark = 0;
			break;
		}
		case ("ul"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_NORTH, false);
			QueuePersonCommand(guy.name, COMMAND_MOVE_WEST, false);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("ur"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_NORTH, false);
			QueuePersonCommand(guy.name, COMMAND_MOVE_EAST, false);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("dl"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_SOUTH, false);
			QueuePersonCommand(guy.name, COMMAND_MOVE_WEST, false);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("dr"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_SOUTH, false);
			QueuePersonCommand(guy.name, COMMAND_MOVE_EAST, false);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("uli"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_NORTH, false);
			QueuePersonCommand(guy.name, COMMAND_MOVE_WEST, true);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("uri"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_NORTH, false);
			QueuePersonCommand(guy.name, COMMAND_MOVE_EAST, true);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("dli"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_SOUTH, false);
			QueuePersonCommand(guy.name, COMMAND_MOVE_WEST, true);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		case ("dri"):
		{
			QueuePersonCommand(guy.name, COMMAND_MOVE_SOUTH, false);
			QueuePersonCommand(guy.name, COMMAND_MOVE_EAST, true);
			if (guy.mark >= 15) { guy.step++; guy.mark = 0; }
			else guy.mark++;
			break;
		}
		default:
		{
			QueuePersonScript(guy.name, dir, false);
			guy.step++;
			guy.mark = 0;
			break;
		}
		}	//end switch
}